<template>
  <div class="AnchorList">
    <el-card>
      <div class="searchBox">
        <div class="left">
          <el-input placeholder="请输入内容" size="small" v-model="user_id">
            <template slot="prepend">主播ID</template>
          </el-input>
          <el-date-picker
            size="small"
            v-model="timeArr"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="timeOptions"
            value-format="yyyy-MM-dd"
            @change="focusA"
          >
          </el-date-picker>
          <el-button
            class="btn ml-20"
            icon="el-icon-search"
            size="small"
            @click="search"
            >搜索</el-button
          >
        </div>
        <div class="right">
          <el-button
            icon="el-icon-circle-plus-outline"
            size="small"
            @click="add"
            >导入主播</el-button
          >
        </div>
      </div>
      <div class="card">
        <h3>
          <span class="point"></span>
          工会宝石收益数
        </h3>
        <h1>
          <i class="iconfont">&#xe65b;</i>
          {{ unionGems }}
        </h1>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%; margin-top: 10px"
        :header-cell-style="{ background: '#f5f7fa' }"
        v-loading="loading"
      >
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column prop="id_code" label="ID"> </el-table-column>
        <el-table-column prop="nick_name" label="昵称"> </el-table-column>
        <el-table-column prop="online_duration" label="在线时长">
        </el-table-column>
        <el-table-column prop="peak_online_duration" label="高峰在线时长">
        </el-table-column>
        <el-table-column prop="matching_duration" label="匹配时长">
        </el-table-column>
        <el-table-column prop="valid_call_time" label="有效通话时长">
        </el-table-column>
        <el-table-column prop="received_voice_num" label="收到语音请求">
        </el-table-column>
        <el-table-column prop="answer_voice_num" label="接听语音请求">
        </el-table-column>
        <el-table-column prop="trends_num" label="发布动态数">
        </el-table-column>
        <el-table-column prop="vaild_call_num" label="有效通话人数">
        </el-table-column>
        <el-table-column prop="diamond_income" label="宝石收益">
        </el-table-column>
      </el-table>
      <br />
      <pagination
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      title="导入主播"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      @close="dialogClose"
    >
      <el-form label-position="right" label-width="120px" :model="form">
        <el-form-item label="主播ID">
          <el-input
            v-model="form.id"
            clearable
            type="textarea"
            autosize
            size="small"
            placeholder="请输入ID"
          ></el-input>
          <div>多个ID使用英文逗号间隔</div>
        </el-form-item>
        <el-form-item label="批量导入EXCEL">
          <upload-excel-component
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import UploadExcelComponent from "@/components/UploadExcel/index.vue";
import {
  AdminUnionUnionAuthorList,
  AdminUnionUnionAdd,
} from "@/api/UnionManagement";
export default {
  components: { Pagination, UploadExcelComponent },
  props: ["id", "phone"],
  data() {
    return {
      loading: false,
      user_id: "",
      unionGems: 0,
      timeArr: [],
      startDate: "",
      endDate: "",
      timeOptions: {
        disabledDate(platformValue) {
          return platformValue.getTime() > Date.now() - 8.64e6;
        },
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      tableData: [],
      total: 0,
      page: 1,
      pageSize: 10,
      dialogVisible: false,
      form: {
        id: "",
      },
    };
  },
  methods: {
    //选着时间
    focusA(time) {
      if (!this.timeArr) {
        this.timeArr = [];
      }
      this.startDate = this.timeArr[0];
      this.endDate = this.timeArr[1];
    },
    //添加主播
    add() {
      this.dialogVisible = true;
    },
    async submit() {
      const res = await AdminUnionUnionAdd({
        id: this.id,
        anchor_id: this.form.id,
      });
      if (res.code == 0) {
        this.$message.success("操作成功！");
        this.getList();
        this.dialogVisible = false;
      }
    },
    //
    handleSuccess({ results, header }) {
      let arr = [...header];
      console.log("结果", results, header);
      results.forEach((item) => {
        arr.push(...Object.values(item));
      });
      this.form.id = arr.join(",");
    },
    //导入表格
    beforeUpload(file) {
      return true;
    },
    dialogClose() {
      this.form.id = "";
    },
    //重置搜索
    resetSearch() {
      this.page = 1;
      this.startDate = "";
      this.endDate = "";
      this.user_id = "";
      this.getList();
    },
    search() {
      this.page = 1;
      this.getList();
    },
    //
    async getList() {
      if (this.id || this.phone) {
        this.loading = true;
        const res = await AdminUnionUnionAuthorList({
          id: this.id,
          page: this.page,
          pageSize: this.pageSize,
          user_id: this.user_id,
          start_time: this.startDate,
          end_time: this.endDate,
          phone: this.phone,
        });
        this.loading = false;
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.total = parseInt(res.data.count);
          this.unionGems = res.data.total;
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.ml-20 {
  margin-left: 20px;
}
.searchBox {
  display: flex;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    .el-input {
      width: 300px;
      margin-right: 20px;
    }
  }
}
.card {
  height: 120px;
  border: 2px solid #ebeff8;
  border-radius: 5px;
  width: 300px;
  margin-top: 20px;
  h3 {
    display: flex;
    align-items: center;
    padding-left: 10px;
    .point {
      display: inline-block;
      width: 20px;
      height: 20px;
      border-radius: 50%;
      border: 5px solid #7b94d8;
      margin-right: 10px;
    }
  }
  h1 {
    margin: 0px 0;
    text-align: right;
    padding-right: 20px;
    i {
      color: #7b94d8;
    }
  }
}
</style>